<?php
/**
 * 公共function
 */

# 获取配置文件信息
$GLOBALS['config'] = include(ROOT_PATH . 'public/config.php');

function config($key){
    $config = $GLOBALS['config'];
    if ( !empty($config[$key]) ) {
        return $config[$key];
    }
    return null;
}

# 作者:焰哥 - 用于微信接口数据传输的万能函数
function https_request($url, $data = null){
    # 初始化一个cURL会话
    $curl = curl_init();  
    
    //设置请求选项, 包括具体的url
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  //禁用后cURL将终止从服务端进行验证
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);  //设置为post请求类型
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  //设置具体的post数据
    }
    
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);		
    $response = curl_exec($curl);  //执行一个cURL会话并且获取相关回复
    curl_close($curl);  //释放cURL句柄,关闭一个cURL会话
    
    return $response;
}

/**
 * 输出json格式的字符
 * @param int       code 状态码
 * @param array     data 数据
 * @param string    msg  消息
 * @return string   json格式的字符 并终止运行
 */
function echoJson($code, $data, $msg = '')
{
    $return['code'] = $code;
    $return['data'] = $data;
    $return['msg']  = $msg;
    echo json_encode($return, JSON_UNESCAPED_UNICODE);
    exit();
}

/**
 * 接收参数
 * @param  [name] [要接收的参数]
 * @return [mixed] [查询到的数据或者null]
 */
function I($name){
    $name = explode('.', $name);

    # 如果只有一个参数 则查询是否要判断方式
    if ( empty($name[1]) ) {
        if ( $name[0][0] == '?' ) {
            $name[0] = substr($name[0], 1);
            if ( !empty($_POST[$name[0]]) ) {
                return 'post';
            }elseif ( !empty($_GET[$name[0]]) ) {
                return 'get';
            }
        }
        $return = !empty($_REQUEST[$name[0]]) ? $_REQUEST[$name[0]] : null;
        return $return;
    }
    # 判断post||get
    if ( $name[0] == 'post' ) {
        return !empty($_POST[$name[1]]) ? $_POST[$name[1]] : null;
    }else if( $name[0] == 'get' ){
        return !empty($_GET[$name[1]]) ? $_GET[$name[1]] : null;
    }
    return null;
}

/**
 * 根据路由模式生成url
 * @param url_info url参数
 * @return url 生成的url地址
 */
function url($url_info, $param){
    $history    = $GLOBALS['url_info'];
    $history_m  = array_shift($history);
    $history_c  = array_shift($history);
    $history_a  = array_shift($history);

    $url_info = explode('/', $url_info);
    $a = array_pop($url_info);
    $c = array_pop($url_info);
    $c = !empty($c) ? $c : $history_c; 
    $m = array_pop($url_info);
    $m = !empty($m) ? $m : $history_m;
    
    $url_model = config('Route_model');
    $url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
    switch ($url_model) {
        case '1':
            $url .= "?m={$m}&c={$c}&a={$a}";
            if ( !empty($param) ) {
                foreach ($param as $param_k => $param_v) {
                    $url .= "&{$param_k}={$param_v}";
                }
            }
            break;

        case '2':
            $url .= "/{$m}/{$c}/{$a}";
            if ( !empty($param) ) {
                foreach ($param as $param_k => $param_v) {
                    $url .= "/{$param_k}/{$param_v}";
                }
            }
            if ( !empty(config('url_suffix')) ) {
                $url .= config('url_suffix');
            }
            break;

        default:
            $url .= "?m={$m}&c={$c}&a={$a}";
            if ( !empty($param) ) {
                foreach ($param as $param_k => $param_v) {
                    $url .= "&{$param_k}={$param_v}";
                }
            }
            break;
    }
   return $url;
}
/**
 * smarty自定义函数 - url
 */
function smarty_url($param){
    $url_info = $param['url_info'];
    unset($param['url_info']);
    return url($url_info, $param);
}
/**
 * 跳转函数
 */
function goUrl($url, $time = 0){
    header("Refresh:{$time};url={$url}");
    die;
}
